const app = getApp();
Page({
    data: {
        showMsg: false,
        focus: true
    },
    onLoad: function(options) {
        let cid = wx.getStorageSync('cid')
        let location = wx.getStorageSync('location')
        let siteInfo = wx.getStorageSync('siteInfo')
        this.setData({
            history: wx.getStorageSync('searchHistory'),
            cid: cid,
            siteInfo: siteInfo ? siteInfo : '',
            location
        })
    },
    onFocus() {
        this.setData({
            focus: true
        });
    },
    onBlur() {
        this.setData({
            focus: false
        });
    },
    search(evt) {
        var content = evt.detail.value;
        if (!content) {
            return
        };
        this.init();
        this.setData({
            content: content,
        });
        if (content) {
            this.loadData();
        }
    },

    init() {
        this.setData({
            page: 1,
            nextPage: true,
            list: [],
        });
    },

    doSearch() {
        this.init();
        this.loadData();
    },

    clickHistory(evt) {
        this.init();
        
        this.setData({
            content: evt.currentTarget.dataset.content
        });
        this.loadData();
    },
    clearHistory() {
        this.setData({
            history: [],
        })
        wx.removeStorageSync('searchHistory');
    },

    clear() {
        this.setData({
            content: ""
        });
    },

    onInput(evt) {
        this.setData({
            content: evt.detail.value
        });
    },

    loadData() {
        let {
            page = 1, nextPage = true, loading = false, list = []
        } = this.data
        var history = wx.getStorageSync('searchHistory');
        if (!history) {
            history = [];
        }

        if (history.length > 5) {
            history.shift();
        }
        if (history.indexOf(this.data.content) < 0) {
            history.push(this.data.content);
        }
        wx.setStorageSync('searchHistory', history);
        this.setData({
            history: history
        })
        if (!nextPage || loading) {
            return;
        }
        this.setData({
            loading: true
        });
        app.http.get('goods/search', {
            data: {
                page: page,
                search_name: this.data.content,
                cid: this.data.cid,
                location: this.data.location.lng + ',' + this.data.location.lat,
            },
        }).then(res => {
            page++
            loading = false
            list.push(...res.data.list)
            nextPage = res.data.nextPage
            this.setData({
                list,
                nextPage,
                loading,
                page
            });
        })
    },
    toDetail(evt) {
        let cid = this.data.cid
        let goods_id = evt.currentTarget.dataset.id
        let goods_type = evt.currentTarget.dataset.type
        let url = ''
        if (goods_type == 1) {
            url = '/pages/goods/detail?cid=' + cid + '&goods_id=' + goods_id
        } else if (goods_type == 2) {
            url = '/pages/write-off/detail?id=' + goods_id
        } else {
            url = '/pages/supermarket/goods-detail?cid=' + cid + '&goods_id=' + goods_id
        }
        wx.navigateTo({
            url: url,
        })
    },
    onReachBottom() {
        if (this.data.content) {
            this.loadData();
        }
    }
})